package com.modern.js.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modern.js.entity.Department;
import com.modern.js.pojo.AssistPojo;
import com.modern.js.service.IDepartmentService;
import com.modern.js.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-03-23
 */
@RestController
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentService iDepartmentService;


    /**
     *
     * @param page 当前页码
     * @param limit 每页的记录数
     * @return
     */
    @GetMapping("/list")
    public Map getList(Integer page, Integer limit,String deptName,String createTime ){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Department> departmentPage = new Page<Department>();
        departmentPage.setSize(limit);//设置每页记录数
        departmentPage.setCurrent(page);//设置当前页码
        IPage<Department> iPage = iDepartmentService.selectAll(departmentPage,deptName,createTime);
        if (iPage.getTotal()!=0){
            map.put("msg","查询成功！");
        }else {
            map.put("msg","查询失败！");
        }
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());
        map.put("code",0);
        return map;
    }

    /**
     * 添加部门信息
     * @param department
     * @return
     */
    @PostMapping("/add")
    public boolean addDep(Department department){
        department.setCreateTime(StringUtils.getNowTime());
        department.setUpdateTime(StringUtils.getNowTime());
        department.setIsDel(1);//1代表存在 0代表删除
        boolean result = iDepartmentService.save(department);
        return result;
    }

    /**
     * 修改部门信息
     * @param department
     * @return
     */
    @PutMapping("/update")
    public boolean updateDep(Department department){
        department.setUpdateTime(StringUtils.getNowTime());
        boolean result=iDepartmentService.updateById(department);
        return result;
    }

    /**
     * 删除部门信息
     * @param deptId
     * @return
     */
    @DeleteMapping("/del/{deptId}")
    public boolean delDep(@PathVariable Integer deptId){
        System.out.println("id:"+deptId);
        Department department = new Department();
        department.setIsDel(0);
        department.setDeptId(deptId);
        boolean result = iDepartmentService.updateById(department);
        return result;
    }

    @GetMapping("/deptList")
    public List selectList(){
        List<Department> list = new ArrayList<>();
        list = iDepartmentService.list();
        return list;
    }


}
